var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store 

Page({
  data: {
    basic:{
      depart:'部门', 
      departId:'部门Id', 
      workCharger:'易雪松',
      workTeam:'班组',
      teamId:'班组Id',
      workPeops:[],
      workPeopsId:[],
      workNum:'人数',
      stationName:'厂站名称',
      stationId:'',
      deviceName:'设备',
      deviceId:'',
      workAddr:'地点',
      workTask:'内容',
      startTime:'',
      endTime:'',
      eleStatus:0,//停电1不停电0
      workCondition:'',//工作条件
    },
    measures1:[],
    measures2:[],
    measures3:[],
    issuer:'',
    issuerId:'',
    info:'',
    isShowTime: false, // 是否显示时间弹框
    isShowTime2: false, // 是否显示时间弹框
    user_name: 0, // 用户id(告警独有)
    company: '', // 企业用户
    company_id: '', // 企业id
    responsibleImg: 'https://img.yzcdn.cn/vant/cat.jpeg', // 签发人签字
    timeType:'',//选择时间类型
    submit: '', // 保存0；提交1 
    companySelected: {}, //公司选择后的数据
    stationSelected: {}, //厂站选择后的数据
    type: 2, // 类型
  },
  onLoad: function (options) { 
    Store.dispatch({
      type: 'SET_FROM',
      form: {

      }, 
    })

    if ('type' in options){ 
      this.setData({
        type: options.type, // 区分接口
        findDate: $.transformTime(),
      })
    }
  },

  onShow(){
    var form = Store.getState().formInfor.form
    var key = form.key
    if (!form.selected) return
    switch (key) {
      case 'selectTickDpt': // 获取部门信息  
        this.setData({
          companySelected: form.selected,
          "basic.depart": form.selected.depart,
          "basic.departId": form.selected.departId,
          "basic.workPeops": '',
          "basic.workTeam":'',
        })
        break
      case 'selectWorkTeam': // 获取班组  
        this.setData({
          stationSelected: form.selected,
          "basic.workTeam": form.selected.workTeam,
          "basic.teamId": form.selected.teamId,
          "basic.workPeops": '',
        })
        break
      case 'selectWorkPeops'://工作成员
        this.setData({
          "basic.workPeops": form.selected.map((item)=>item.userName),
          "basic.workPeopsId":form.selected.map((item)=>item.userId),
        })
        break
      case 'selectWorkStation': // 获取厂站  
        this.setData({
          stationSelected: form.selected,
          "basic.stationName": form.selected.dept_name,
          "basic.stationId": form.selected.dept_id,
        })
        break
      case 'selectWorkDevice': // 获取设备  
        this.setData({
          stationSelected: form.selected,
          "basic.deviceName": form.selected.YCXLM,
          "basic.deviceId": form.selected.YCXLM,
        })
        break
      case 'getMeasures1': // 获取安措1  
        this.setData({
          measures1: form.selected,
        })
        break
      case 'selectIssuer': // 获取签发人  
        this.setData({
          issuer: form.selected.userName,
          issuerId: form.selected.userId,
        })
        break 
      case 'responsibleSign': // 获取签发人签名  
        this.setData({
          responsibleImg: form.selected,
        })
        break 
        
    }
    console.log(form,this.data.responsibleImg) 
  },

  selectDepartment() { // 部门  
    // if(this.data.type!=2){
    //   $.toast("该内容无法修改");
    //   return
    // }
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectTickDpt', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)  
  },

  selectWorkTeam() { // 选班组
    if(this.data.basic.depart== ''){
      $.toast('请选择所属部门')
    }else {
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectWorkTeam', // 后台要的更改字段 
          type: 1, // 表单类型 1： radio  2：select
          config: {
            depart: this.data.basic.depart, 
            departId: this.data.basic.departId ,
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
    } 
  },
  selectWorkPeops() { // 选抢修负责人或者抢修人员
    if(this.data.info.submit=='1'){
      $.toast("已提交无法修改")
    }else{
      Store.dispatch({
        type: 'SET_FROM',
        form: {
          key: 'selectWorkPeops', // 后台要的更改字段 
          type:2, // 表单类型 1： radio  2：select
          config: {
            dataType:'object',
            teamId: this.data.basic.teamId ,
          },
        },
      })
      $.linkurl(`/pages/main/chooseForm/chooseForm`)
    }
  },
  changeWorkNum(){
    var that=this
    wx.showModal({
      title: '参加人数',
      editable: true,
      placeholderText:'请输入数字',
      success (res) {
        if (res.confirm) {
          if(isNaN(res.content)){
            $.toast("请输入阿拉伯数字");
            
          }else{
            that.setData({
              "basic.workNum":res.content
            })
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  selectWorkStation(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectWorkStation', // 后台要的更改字段 
        type: 1, // 表单类型 1： radio  2：select
        config: {
          company: '全盟电力园区', 
          cId: 398,
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  selectWorkDevice (e) { // 根据厂站获取设备列表 
    if (this.data.type!='add'&&this.data.basic.status==6) {
      $.toast('已生成票号无法修改，如需换站请重新新增操作票')
      return false
    }
    if (this.data.basic.stationName == '') {
      $.toast('请选择厂站')
    } else {
      api.$post(api.main.selectWorkStation, {dept_id:398,company:'全盟电力园区'}).then((res) => {
        var result = res.data.find((item) => {
          console.log(item.dept_name,this.data.basic.stationName)
          return item.dept_name == this.data.basic.stationName
        }) 
        var { type } = $.detail(e)
        Store.dispatch({
          type: 'SET_FROM',
          form: {
            key: 'selectWorkDevice', // 后台要的更改字段 
            type: 1, // 表单类型 1： radio  2：select
            config: {
              station: result.station
            },
          },
        })
        $.linkurl(`/pages/main/chooseForm/chooseForm`)
      })
      
    } 
  },
  changeWorkAddr(e){
    console.log(e.detail.value)
    this.setData({'basic.workAddr':e.detail.value})
  },
  changeWorkTask(e){
    this.setData({'basic.workTask':e.detail.value})
  },
  openTimeHandle(e) { // 打开时间弹框
    const {item}=$.detail(e)
    this.setData({
      timeType:item
    })
    if(this.data.info.submit=='1'){
      $.toast("已提交无法修改")
  }else{
    if(item=='startTime'||item=='pzStartTime'){
      this.setData({
        isShowTime: true
      })
    }else if((item=='endTime'&&this.data.basic.startTime!='')||item=='pzEndTime'&&this.data.pzEndTime!=''){
      this.setData({
        isShowTime2: true
      })
    }else{
      $.toast("请先选择开始时间")
    }
      
    }
  },
  timeConfirmHandle(e) { // 时间组件传出来的数据
    var { time, key } = e.detail

    this.setData({
      [key]: time
    })
  
    console.log(time, key)
  },
  getEleStatus(){
    console.log('oooo')
    var that=this
    wx.showActionSheet({
      itemList: ['不停电', '停电'],
      success (res) {
        that.setData({
          "basic.eleStatus":res.tapIndex
        })
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  getMeasures(e){
    const {type}=$.detail(e)
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'getMeasures'+type, // 后台要的更改字段 
        type:'0',
        config: {
          deviceName:this.data.basic.deviceName, 
          deviceId: this.data.basic.deviceId,
        },
      },
    })
    $.linkurl(`/pages/workTicket/workTicketMeasures/workTicketMeasures`) 
  },
  bigSign(e){
    const {img}=$.detail(e)
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [img, 'https://example.com/2.jpg'] // 需要预览的图片链接列表
    })
  },
  responsibleSign(e){  
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'responsibleSign', 
      },
    })
    $.linkurl(`/pages/components/signatrue/signAture`) 
  },
  selectIssuer(){
    Store.dispatch({
      type: 'SET_FROM',
      form: {
        key: 'selectIssuer', // 签发人
        type:1, // 表单类型 1： radio  2：select
        config: {
          teamId: this.data.basic.teamId ,
        },
      },
    })
    $.linkurl(`/pages/main/chooseForm/chooseForm`)
  },
  cancelMake(){
    $.linkurl('','back')
  },
  subTicket(){
    $.linkurl('','back')
  }



})